#include "stdafx.h"
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/TransService.hpp>

XERCES_CPP_NAMESPACE_USE

namespace xml
{
	SAX2Handler::SAX2Handler()
	{}
	SAX2Handler::~SAX2Handler()
	{}
	void SAX2Handler::error(const SAXParseException& e)
	{
		XERCES_STD_QUALIFIER cerr << "\nError at file " << X2C(e.getSystemId())
				<< ", line " << e.getLineNumber()
				<< ", char " << e.getColumnNumber()
				<< "\n  Message: " << X2C(e.getMessage()) << XERCES_STD_QUALIFIER endl;
	}
	void SAX2Handler::fatalError(const SAXParseException& e)
	{
		XERCES_STD_QUALIFIER cerr << "\nFatal Error at file " << X2C(e.getSystemId())
				<< ", line " << e.getLineNumber()
				<< ", char " << e.getColumnNumber()
				<< "\n  Message: " << X2C(e.getMessage()) << XERCES_STD_QUALIFIER endl;
	}
	void SAX2Handler::warning(const SAXParseException& e)
	{
		XERCES_STD_QUALIFIER cerr << "\nWarning at file " << X2C(e.getSystemId())
				<< ", line " << e.getLineNumber()
				<< ", char " << e.getColumnNumber()
				<< "\n  Message: " << X2C(e.getMessage()) << XERCES_STD_QUALIFIER endl;
	}

	void SAX2Handler::startElement(	const XMLCh* const uri,const XMLCh* const localname,const XMLCh* const qname,const Attributes& attributes)
	{}
	void SAX2Handler::characters( const XMLCh* const chars, const XMLSize_t length )
	{}
	void SAX2Handler::endElement( const XMLCh* const uri, const XMLCh* const localname,const XMLCh* const qname )
	{}
	void SAX2Handler::processingInstruction(const  XMLCh* const target, const XMLCh* const data)
	{}
	void SAX2Handler::startEntity(const XMLCh* const name)
	{}
	void SAX2Handler::elementDecl(const XMLCh* const name, const XMLCh* const    model)
	{}
	void SAX2Handler::attributeDecl(const XMLCh* const eName, const XMLCh* const aName, const XMLCh* const type, const XMLCh* const mode, const XMLCh* const value)
	{}
	void SAX2Handler::internalEntityDecl(const XMLCh* const name, const XMLCh* const value)
	{}
	void SAX2Handler::externalEntityDecl(const XMLCh* const name, const XMLCh* const publicId, const XMLCh* const systemId)
	{}
	void SAX2Handler::ignorableWhitespace(const XMLCh* const chars, const XMLSize_t length)
	{}
	void SAX2Handler::setDocumentLocator(const Locator* const locator)
	{}
	void SAX2Handler::startPrefixMapping (const	XMLCh* const prefix,const XMLCh* const uri)
	{}
	void SAX2Handler::endPrefixMapping(const	XMLCh* const	prefix)
	{}
	void SAX2Handler::skippedEntity(const	XMLCh* const	name)
	{}
	InputSource* SAX2Handler::resolveEntity(const   XMLCh* const publicId, const XMLCh* const    systemId)
	{
		return 0;
	}
	void SAX2Handler::resetDocType()
	{}
	void SAX2Handler::endDocument()
	{}
	void SAX2Handler::resetDocument()
	{}
	void SAX2Handler::notationDecl(const XMLCh* const name, const XMLCh* const publicId, const XMLCh* const systemId)
	{}
	void SAX2Handler::unparsedEntityDecl(const   XMLCh* const    name, const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const notationName)
	{}
	void SAX2Handler::startDocument()
	{}
	void SAX2Handler::resetErrors()
	{}
	void SAX2Handler::comment( const   XMLCh* const chars, const XMLSize_t length)
	{}
	void SAX2Handler::endCDATA()
	{}
	void SAX2Handler::endDTD()
	{}
	void SAX2Handler::endEntity(const XMLCh* const name)
	{}
	void SAX2Handler::startCDATA()
	{}
	void SAX2Handler::startDTD(const XMLCh* const    name, const XMLCh* const publicId, const XMLCh* const systemId)
	{}

	SAX2Reader::SAX2Reader( const string& xmlFile, SAX2Handler* pSAX2Handler ):m_xmlFile(xmlFile), m_pSAX2Handler( pSAX2Handler )
	{}
		
	bool SAX2Reader::Read()
	{
		//  Create a SAX parser object. Then, according to what we were told on
		//  the command line, set it to validate or not.
		SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();

		//  Then, according to what we were told on
		//  the command line, set it to validate or not.
		parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
		parser->setFeature(XMLUni::fgXercesDynamic, true);

		//parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, doNamespaces);
		//parser->setFeature(XMLUni::fgXercesSchema, doSchema);
		parser->setFeature(XMLUni::fgXercesHandleMultipleImports, true);
		//parser->setFeature(XMLUni::fgXercesSchemaFullChecking, schemaFullChecking);
		//parser->setFeature(XMLUni::fgSAX2CoreNameSpacePrefixes, namespacePrefixes);

		int errorCount = 0;
		int errorCode = 0;
		try
		{
			parser->setContentHandler( m_pSAX2Handler );
			parser->setErrorHandler( m_pSAX2Handler );
			parser->parse( m_xmlFile.c_str() );
			errorCount = parser->getErrorCount();
		}
		catch (const OutOfMemoryException&)
		{
			m_strError << "OutOfMemoryException";
		}
		catch (const XMLException& toCatch)
		{
			m_strError << "Error: " << xml::X2C( toCatch.getMessage() );
		}
		delete parser;
		if ( errorCount == 0 )
			return true;
		return false;
	}
	string SAX2Reader::GetError()
	{
		return m_strError.str();
	}
}